<template>
  <div class="swiper-container" ref="someRef">
    <div class="swiper-wrapper">
      <div
        class="swiper-slide"
        v-for="carouse in someList"
        :key="carouse.id"
      >
        <img :src="carouse.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
export default {
  name: "Carousel",
  props: ["someList", "someRef"],
  watch: {
    someList: {
      handler(newVal, oldVal) {
        this.$nextTick(() => {
          new Swiper(this.$refs.someRef, {
            spaceBetween: 30, //控制每一屏中间的间隔
            speed: 1000,
            loop: true,
            autoplay: {
              delay: 1000,
              disableOnInteraction: false, //鼠标操作之后 是否禁止自动轮播
            },
            pagination: {
              el: ".swiper-pagination", //选择到那个小圆点元素
              clickable: true, // 是否可以点击小圆点
            },
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
      // 这里加个immediate:true   意思是进来之后，立马执行。  虽然handler会立即执行，但是由于nexttick的存在，还是会等结构渲染好之后再来执行Swiper这个微任务
      immediate: true,
    },
  },
};
</script>

<style lang="less" scoped>
</style>
